package com.jindi.appuser.controller;

import com.jindi.appuser.common.EmptyUtil;
import com.jindi.appuser.common.RandomString;
import com.jindi.appuser.common.ServerResponse;
import com.jindi.appuser.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("app/pay_price")
public class PricePayController {

    @Autowired
    private PayService payService;
    /**
     * 支付宝支付
     * @param uid
     * @param
     * @param total_amount 订单总金额，单位元，精确到小数点后两位
     * @param timeout_express 订单允许的最晚付款时间 m分钟 d天 空默认15d
     * @return
     */
    @PostMapping("/payByAli.do")
    public ServerResponse payByAli(@RequestParam(value = "uid",required = false) String uid,
                                   @RequestParam(value = "os",defaultValue = "1",required = false) String os,
                                   @RequestParam(value = "total_amount",required = false) String total_amount,
                                   @RequestParam(value = "priceId",required = false) String priceId,
                                   @RequestParam(value = "timeout_express",defaultValue = "30m",required = false) String timeout_express){
        if(!EmptyUtil.checkParam(uid,total_amount,os,priceId)){
            return ServerResponse.createByErrorMessage("参数有误");
        }
        return payService.payByAliPrice(uid,os,total_amount,timeout_express,priceId);
    }

    /**
     * 支付宝服务端验签
     * @param request
     * @return
     */
    @PostMapping(value = "/checkAliSignPrice.do")
    public String checkAliSign(HttpServletRequest request) {
        return payService.checkAliSignPrice(request);
    }
    /**
     * 微信支付
     * @param uid
     * @param
     * @param total_amount 订单金额
     * @return
     */
    @PostMapping("/payByWechat.do")
    public ServerResponse payByWechat(@RequestParam(value = "uid",required = false) String uid,
                                      @RequestParam(value = "os",defaultValue = "1",required = false) String os,
                                      @RequestParam(value = "total_amount",required = false) String total_amount,
                                      @RequestParam(value = "priceId",required = false) String priceId,
                                      HttpServletRequest request){
        if(EmptyUtil.isEmpty(uid)){
            return ServerResponse.createByErrorMessage("系统错误");
        }
        if(EmptyUtil.isEmpty(total_amount) || EmptyUtil.isEmpty(priceId)){
            return ServerResponse.createByErrorMessage("请选择您要购买的套餐");
        }
        return payService.payByWechatPrice(uid,os,total_amount,request,priceId);
    }

    /**
     * 微信服务端验签
     * @param request
     * @return
     */
    @PostMapping(value = "/checkWechatSign.do")
    public String checkWechatSign(HttpServletRequest request) {
        return payService.checkWechatSignPrice(request);
    }

}
